package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.entify.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 用户地址新增
     */
    @PostMapping
    public R save(@RequestBody AddressBook addressBook, HttpSession session) {
        //登录人的id
        Long userId = UserHolder.getUserId();
        addressBook.setUserId(userId);
        boolean save = addressBookService.save(addressBook);
        return save ? R.success("新增地址成功") : R.error("新增地址失败");
    }

    /**
     * 设置默认地址
     */
    @PutMapping("/default")
    public R setDefault(@RequestBody AddressBook addressBook) {
        //先将该用户所有地址的is_default更新为0；
        LambdaUpdateWrapper<AddressBook> qw = new LambdaUpdateWrapper<>();
        qw.eq(AddressBook::getUserId, UserHolder.getUserId());
        qw.set(AddressBook::getIsDefault, 0);
        addressBookService.update(qw);
        //设置新的默认地址
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success(addressBook);
    }

    /**
     * 根据id查询地址
     */
    @GetMapping("/{id}")
    public R getById(@PathVariable Long id){
        AddressBook addressBook = addressBookService.getById(id);
        //判断addressBok是否为空
        if(addressBook!=null){
            return R.success(addressBook);
        }else{
            return R.error("没有查询到该对象");
        }
    }

    /**
     * 查询默认地址
     */
    @GetMapping("/default")
    public R getDefault(){
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.eq(AddressBook::getUserId,UserHolder.getUserId());
        qw.eq(AddressBook::getIsDefault,1);
        AddressBook one = addressBookService.getOne(qw);
        if(one!=null){
            return R.success(one);
        }else{
            return R.error("没有查询到默认地址");
        }
    }
    /**
     * 查询指定用户的全部地址
     */
    @GetMapping("/list")
    public R list(AddressBook addressBook){
        Long userId = UserHolder.getUserId();
        addressBook.setUserId(userId);

        //条件构造器
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.eq(null != addressBook.getUserId(),AddressBook::getUserId,addressBook.getUserId());
        List<AddressBook> list = addressBookService.list(qw);

        return R.success(list);
    }

    /**
     * 删除地址
     */
    @DeleteMapping
    public R deleteByIds(@RequestParam List<Long> ids){

        addressBookService.deleteByIds(ids);

        return R.success("删除成功");
    }

    /**
     * 修改地址的接口
     */
    @PutMapping
    public R update(@RequestBody AddressBook addressBook){

        boolean result = addressBookService.updateById(addressBook);

        return result?R.success("修改地址成功"):R.error("修改地址失败");
    }

}
